<template>
	<view>
		<nav-bar title="门店详情"></nav-bar>
		<!-- <scroll-view scroll-y :style="{ height: scrollHeight }" @scroll="outerScroll"> -->
		<view class="store-info-box">
			<view class="store-info">
				<view class="store-img">
					<image class="img" :src="'http://47.121.199.55:8088' + storeDetail.images" mode="aspectFill" />
				</view>
				<view class="info-box">
					<view class="name">{{ storeDetail.storeName }}</view>
					<view class="phone-number">{{ storeDetail.phoneNumber }}</view>
					<view class="label-box">
						<view class="label" v-for="(item, index) in storeDetail.tags" :key="index">{{ item }}
						</view>
					</view>
					<view class="search-btn" @click="clickSearchStore">
						<image class="img" :src="baseIconUrl + 'icon_search.png'" mode="aspectFill" />
						搜本店
					</view>
				</view>
			</view>
			<view class="store-time">营业时间<view class="time">{{ storeDetail.openingTime }} -
					{{ storeDetail.closingTime }}
				</view>
			</view>
			<view class="store-address">
				<view class="left-data">
					<view class="num-box">
						<!-- <view class="distance">{{ storeDetail.deliveryScope }}km</view> -->
						<view class="deliveryFee">配送费￥{{ storeDetail.freight }}</view>
					</view>
					<view class="address">{{ storeDetail.detail }}</view>
				</view>
				<!-- <view class="navigate-box">
					<image class="navigate-icon" :src="baseIconUrl + 'icon_navigation.png'" mode="scaleToFill" />
					导航
				</view> -->
			</view>
		</view>
		<you-side-navigate :innerScrollable="innerScrollable" :scrollBoxHeight="scrollHeight"
			:leftMenuList="goodsTypeList" :rightContentList="typeGoodsList" :storeId="storeId"
			@clickMenuItem="clickMenuItem"></you-side-navigate>
		<!-- </scroll-view> -->
		<view class="bottom-box" :style="{ paddingBottom: safeHeight + 5 + 'px' }">
			<view class="car-box" @click="clickCar">
				<image class="car-icon" :src="baseIconUrl + 'icon_car.png'" mode="aspectFill" />购物车
			</view>
			<view class="text-box" v-if="shop_car.selectCartList.length > 0">
				已选{{ shop_car.selectCartList.length }}件，合计
				<text class="account-icon">￥</text>
				<text class="account">{{ goodsAccount.toFixed(2) }}</text>
			</view>
			<view class="btn-box" @click="clickSettle">去结算</view>
		</view>
	</view>
</template>

<script>
	import {
		baseUrl,
		baseImgUrl,
		baseIconUrl
	} from '@/config'

	export default {
		name: 'StoreDetail',
		data() {
			return {
				baseUrl,
				baseImgUrl,
				baseIconUrl,
				scrollHeight: '',
				safeHeight: '',
				storeDetail: {},
				goodsNum: 0,
				goodsTypeList: [],
				typeGoodsList: [],
				innerScrollable: false,
				storeId: '',
				shop_car: {
					cartList: [],
					selectCartList: []
				}
			}
		},
		computed: {
			goodsAccount() {
				const selectKey = "id"
				const selectedList = this.shop_car.cartList.filter(item => this.shop_car.selectCartList.includes(item[
					selectKey]))
				const selectedPrice = selectedList.reduce((retailPrice, item) => {
					return retailPrice + item.count * item.retailPrice
				}, 0)
				return selectedPrice
			}
		},
		onLoad(options) {
			this.storeId = options.storeId
			this.safeHeight = this.$store.getters.heightData.safeHeight
			// this.getGoodsType()
			// this.getAllcategory()
			// this.getTypeGoodsList()
			this.getStoreInfo()
		},
		onShow() {
			const shop_car = this.$store.getters.shop_car
			console.log(shop_car)
			this.shop_car = shop_car
		},
		onReady() {
			this.getBottomBoxHeight()
		},
		methods: {
			//左侧选项卡变更
			clickMenuItem(e) {
				const rightArr = this.goodsTypeList[e.index].children.map((item, index) => {
					item.subchildren.forEach(items => {
						items.name = items.deptName
						items.key = items.id
						items.img = `${this.baseUrl}${items.classifyImg}`
					})
					item.title = item.deptName
					// item.itemList = item.goodList
					item.typeKey = index
					return item
				})
				this.typeGoodsList = rightArr
			},
			async getAllcategory(storeId) {
				const res = await this.$GET(`/fx/store/good/classifyList?storeId=${storeId}`)
				if (res.code == 200) {
					const result = this.$utils.organizeArray(res.data)
					console.log(result)
					const leftArr = result.map((item, index) => {
						item.key = index
						item.name = item.deptName
						return item
					})
					const rightArr = result[0].children.map((item, index) => {
						item.subchildren.forEach(items => {
							items.name = items.deptName
							items.key = items.id
							items.img = `${this.baseUrl}${items.classifyImg}`
						})
						item.title = item.deptName
						// item.itemList = item.goodList
						item.typeKey = index
						return item
					})
					this.goodsTypeList = leftArr
					this.typeGoodsList = rightArr
				}
			},
			async getStoreInfo() {
				const res = await this.$GET(`/fx/store/${this.storeId}`)
				if (res.code == 200) {
					console.log(res)
					if (res.data.tegNames != null) {
						res.data.tags = res.data.tegNames.split(',')
					}
					this.storeDetail = {
						...res.data
					}
					console.log(this.storeDetail.storeQrCodeUrl);
					this.$store.commit('SET_DELIVERY', res.data.freight)
					this.$store.commit('SET_DELIVERY2', res.data.urgentCharge)
					this.getAllcategory(res.data.id)
				}
			},
			getBottomBoxHeight() {
				this.$nextTick(() => {
					// 创建查询对象
					setTimeout(() => {
						const query = uni.createSelectorQuery().in(this)

						// 选择我们想要的元素
						query.select('.bottom-box').boundingClientRect(data => {
							// data是一个包含元素尺寸信息的对象
							if (data) {
								// 获取元素高度
								const height = data.height
								this.scrollHeight = this.$store.getters.heightData
									.noTabMainHeight - height + this.safeHeight + 'px'
							}
						}).exec()
					}, 200)
				})
			},
			outerScroll(e) {
				// 计算外部 scroll-view 滚动位置
				const scrollTop = e.detail.scrollTop
				const scrollHeight = e.detail.scrollHeight
				// 判断是否滚动到底部
				if (scrollTop + this.$store.getters.heightData.noTabMainHeight >= scrollHeight) {
					// 滚动到底部，允许内部 scroll-view 滚动
					this.innerScrollable = true
				} else {
					// 没有滚动到底部，禁止内部 scroll-view 滚动
					this.innerScrollable = false
				}
			},
			getGoodsType() {
				this.goodsTypeList = [{
						name: '商品分类1',
						key: 'type1'
					},
					{
						name: '商品分类2',
						key: 'type2'
					},
					{
						name: '商品分类3',
						key: 'type3'
					},
					{
						name: '商品分类4',
						key: 'type4'
					},
					{
						name: '商品分类5',
						key: 'type5'
					},
					{
						name: '商品分类6',
						key: 'type6'
					},
					{
						name: '商品分类7',
						key: 'type7'
					},
					{
						name: '商品分类8',
						key: 'type8'
					},
					{
						name: '商品分类9',
						key: 'type9'
					},
					{
						name: '商品分类10',
						key: 'type10'
					},
					{
						name: '商品分类11',
						key: 'type11'
					},
					{
						name: '商品分类12',
						key: 'type12'
					},
					{
						name: '商品分类13',
						key: 'type13'
					},
					{
						name: '商品分类14',
						key: 'type14'
					},
					{
						name: '商品分类15',
						key: 'type15'
					},
					{
						name: '商品分类16',
						key: 'type16'
					},
					{
						name: '商品分类17',
						key: 'type17'
					},
					{
						name: '商品分类18',
						key: 'type18'
					},
					{
						name: '商品分类19',
						key: 'type19'
					},
					{
						name: '商品分类20',
						key: 'type20'
					},
					{
						name: '商品分类21',
						key: 'type21'
					},
					{
						name: '商品分类22',
						key: 'type22'
					}
				]
			},
			async getTypeGoodsList() {
				const data = {
					storeId: this.storeId
				}
				const res = await this.$GET('/fx/store/good/classifyList', data)
				if (res.code == 200) {
					const rightArr = res.data.map((item, index) => {
						item.goodList.forEach(items => {
							items.name = items.goodName
							items.key = items.id
							items.img = items.goodImg
						})
						item.title = item.deptName
						item.itemList = item.goodList
						item.typeKey = index
						return item
					})
					const leftArr = res.data.map((item, index) => {
						item.key = index
						item.name = item.deptName
						return item
					})
					this.typeGoodsList = rightArr
					this.goodsTypeList = leftArr
				}
			},
			clickSearchStore() {
				uni.navigateTo({
					url: `/subPages/store/storeSearch?storeId=${this.storeId}`
				})
			},
			clickCar() {
				uni.navigateTo({
					url: `/subPages/store/shoppingCart?storeId=${this.storeId}`
				})
			},
			clickSettle() {
				if (this.shop_car.selectCartList.length < 1) {
					uni.showToast({
						icon: 'none',
						title: '当前购物车为空'
					})
				} else {
					const selectKey = "id"
					const data = this.shop_car.cartList.filter(item => this.shop_car.selectCartList.includes(item[
						selectKey]))
					const storeId = this.storeId
					this.$store.commit('SET_SETTLE', data)
					uni.navigateTo({
						url: `/subPages/store/confirmOrder?storeId=${storeId}`
					})
				}
			}
		}
	}
</script>
<style lang="scss" scoped>
	.store-info-box {
		margin: 32rpx 0 20rpx;
		padding: 20rpx 28rpx;
		border-radius: 24rpx 24rpx 0 0;
		background-color: #FFF;

		.store-info {
			display: flex;

			.store-img {
				width: 140rpx;
				height: 140rpx;
				margin-right: 14rpx;
				border-radius: 12rpx;
				overflow: hidden;
				background-color: #e8f0ee;

				.img {
					width: 100%;
					height: 100%;
				}
			}

			.info-box {
				position: relative;
				flex: 1;

				.name {
					height: 38%;
					font-weight: 500;
					font-size: 36rpx;
				}

				.phone-number {
					margin: 10rpx 0;
					font-size: 24rpx;
				}

				.label-box {
					display: flex;
					align-items: center;

					.label {
						padding: 6rpx;
						margin-right: 10rpx;
						border-radius: 4rpx;
						font-size: 20rpx;
						color: #126352;
						background-color: rgba(18, 99, 82, .1);

						&:last-child {
							margin-right: 0;
						}
					}
				}

				.search-btn {
					position: absolute;
					right: 0;
					bottom: 0;
					height: 50rpx;
					padding: 8rpx 12rpx;
					border-radius: 8rpx;
					font-size: 24rpx;
					color: #FFF;
					background-color: #0b3f35;
					display: flex;

					.img {
						width: 34rpx;
						height: 34rpx;
					}
				}
			}
		}

		.store-time {
			display: flex;
			align-items: center;
			margin: 20rpx 0;
			font-weight: 500;
			color: #808080;

			.time {
				margin-left: 20rpx;
				font-weight: 400;
			}
		}

		.store-address {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-top: 20rpx;
			border-top: 2rpx solid #e6e6e6;

			.left-data {
				.num-box {
					display: flex;
					align-items: center;
					font-size: 24rpx;
					font-weight: 500;

					.distance {
						margin-right: 20rpx;
					}
				}

				.address {
					margin-top: 20rpx;
					color: #808080;
				}
			}

			.navigate-box {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				font-size: 20rpx;
				color: #808080;

				.navigate-icon {
					width: 48rpx;
					height: 48rpx;
					margin-bottom: 12rpx;
				}
			}
		}
	}

	.bottom-box {
		position: fixed;
		bottom: 0;
		display: flex;
		padding: 10rpx 28rpx 0;
		align-items: center;
		width: 100%;
		border-top: 2rpx solid rgba(0, 0, 0, .1);
		background-color: #FFF;
		flex-flow: row nowrap;
		justify-content: space-between;

		.car-box {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 20rpx;
			color: #808080;

			.car-icon {
				width: 48rpx;
				height: 48rpx;
				margin-bottom: 4rpx;
			}
		}

		.text-box {
			flex: 1;
			margin-left: 46rpx;
			font-size: 24rpx;

			.account-icon {
				margin-left: 8rpx;
				color: #EB2727;
			}

			.account {
				color: #EB2727;
				font-size: 26rpx;
			}
		}

		.btn-box {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 180rpx;
			height: 80rpx;
			border-radius: 40rpx;
			font-size: 30rpx;
			color: #FFF;
			background-color: #0b3f35;
		}
	}
</style>